﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class ManagerLoginLogController :UI.BaseController
    {
        public const string ProgrameCode = "ManagerLoginLog";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public ManagerLoginLogController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.ManagerLoginLog), "LoginStatus,UserName,LoginIP");
            string orderby = "LoginTime desc";
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.ManagerLoginLog();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    ID = i.ID,
                    UserName = i.UserName,
                    RealName = i.Manager != null ? i.Manager.RealName : null,
                    LoginIP = i.LoginIP,
                    LoginTime = UI.Utils.FormateDate(i.LoginTime),
                    LoginStatus =i.LoginStatus
                });
            }            
            
            var ret = new
            {
                total=bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.ManagerLoginLog().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

    }
}
